package high.concurent.controller;

import com.google.code.kaptcha.Producer;
import high.concurent.service.NotifyService;
import high.concurent.util.CaptchaUtil;
import high.concurrent.enums.SendCodeEnum;
import high.concurrent.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api/notify/v1/")
@Api(tags = "信息验证模块")
public class NotifyController {

    @Autowired
    private NotifyService notifyService;

    @Autowired
    private Producer captchaProducer;

    /**
     * 获取图片验证码
     * @param response
     * @param request
     */
    @ApiOperation("获取图形验证码")
    @GetMapping("getCaptcha")
    public void getCaptcha(HttpServletResponse response, HttpServletRequest request){

        // 获取当前【机器ip】及【指纹认证】
        String captchaKey = CaptchaUtil.getCaptchaKey(request);
        notifyService.getCaptcha(captchaProducer, response, captchaKey);
    }

    /**
     * 发送验证码（手机/邮箱）
     * @param to         手机号 或者 邮箱
     * @param captcha    图片验证码
     * @return
     */
    @ApiOperation("发送验证码(手机/邮箱)")
    @GetMapping("send_code")
    public JsonData sendCode(HttpServletRequest requestr,
                             @ApiParam(value = "邮箱或者手机号", required = true)@RequestParam(value = "to") String to,
                             @ApiParam(value = "图片验证码", required = true) @RequestParam(value = "captcha") String captcha){

        // 获取当前机器的ip+指纹
        String captchaKey = CaptchaUtil.getCaptchaKey(requestr);
        // 发送验证码，并指明是注册验证码
        JsonData jsonData = notifyService.sendCode(SendCodeEnum.USER_REGISTER, to, captcha, captchaKey);

        return jsonData;
    }
}
